package com.renwei.yunlong.adapter;


import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.bean.UpcomingAllBean;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;

import java.util.List;

/**
 * 服务商 我的待办工单--adapter
 *
 * Created by Administrator on 2017/9/6.
 */

public class ServiceMeUpcomingWorkAdapter extends RecyclerView.Adapter<ServiceMeUpcomingWorkAdapter.ServiceMeUpcomingWorkViewHolder>{

    LayoutInflater inflater;
    Context context;

    List<UpcomingAllBean.Rows> mList;

    private OnItemClickListener mOnItemClickListener;

    public ServiceMeUpcomingWorkAdapter(Context context, List<UpcomingAllBean.Rows> mList){
        this.context = context;
        this.mList = mList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public ServiceMeUpcomingWorkViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.item_me_upcoming_work,null);
        AutoUtils.auto(view);
        return new ServiceMeUpcomingWorkViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ServiceMeUpcomingWorkViewHolder holder, int position) {
        final UpcomingAllBean.Rows rows = mList.get(position);
        holder.tvTitleContent.setText(rows.getContent());
        holder.tvDepartment.setText(rows.getServerName());//serverName
        holder.tvName.setText("/"+rows.getSendUserName());

        if ("1".equals(rows.getIsOverStat())){
            holder.rlDateTime.setVisibility(View.GONE);
            holder.tvOverdue.setVisibility(View.VISIBLE);

            holder.tvOverdue.setText("逾期");
        }else {
            holder.tvOverdue.setVisibility(View.GONE);

            String DateTimeStr = rows.getSendDate();
            String [] split = DateTimeStr.split("T");

            holder.rlDateTime.setVisibility(View.VISIBLE);
            holder.tvDate.setText(split[0]);
            holder.tvTime.setText(split[1]);
        }

        if ("1".equals(rows.getKind())){
            if ("0".equals(rows.getComplainFlag())){
                holder.button.setBackgroundResource(R.drawable.shape_red_card);
                holder.button.setText("未指派");
            }else if ("3".equals(rows.getComplainFlag())){
                holder.button.setBackgroundColor(Color.parseColor("#76c56d"));
                holder.button.setText("已指派");
            }else if ("4".equals(rows.getComplainFlag())){
                holder.button.setBackgroundColor(Color.parseColor("#e9981f"));
                holder.button.setText("处理中");
            }else if ("5".equals(rows.getComplainFlag())){
                holder.button.setBackgroundColor(Color.parseColor("#999691"));
                holder.button.setText("已关闭");
            }else if ("7".equals(rows.getComplainFlag())){
                holder.button.setBackgroundColor(Color.parseColor("#e15656"));
                holder.button.setText("挂起");
            }else if ("9".equals(rows.getComplainFlag())){
                holder.button.setBackgroundColor(Color.parseColor("#e15656"));
                holder.button.setText("待确认");
            }else if ("10".equals(rows.getComplainFlag())){
                holder.button.setBackgroundColor(Color.parseColor("#76c56d"));
                holder.button.setText("已解决");
            }else if ("11".equals(rows.getComplainFlag())){
                holder.button.setBackgroundColor(Color.parseColor("#999691"));
                holder.button.setText("已取消");
            }else if ("16".equals(rows.getComplainFlag())){
                holder.button.setBackgroundColor(Color.parseColor("#e15656"));
                holder.button.setText("出发中");
            }else if ("17".equals(rows.getComplainFlag())){
                holder.button.setBackgroundColor(Color.parseColor("#76c56d"));
                holder.button.setText("已到达");
            }else if ("18".equals(rows.getComplainFlag())){
                holder.button.setBackgroundColor(Color.parseColor("#e15656"));
                holder.button.setText("未指派");
            }
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getLayoutPosition();
                mOnItemClickListener.onItemClick(holder.itemView,pos,rows.getRequestId());
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){

        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position,String requestId);
    }

    @Override
    public int getItemCount() {
        return mList == null?0:mList.size();
    }


    static class ServiceMeUpcomingWorkViewHolder extends RecyclerView.ViewHolder{
        private View itemView;
        private TextView tvTitleContent;
        private TextView tvDepartment;
        private TextView tvName;
        private TextView tvTime;
        private TextView tvOverdue;
        private Button button;

        private TextView tvDate;
        private RelativeLayout rlDateTime;

        public ServiceMeUpcomingWorkViewHolder(View view) {
            super(view);
            itemView = view;
            tvTitleContent = (TextView) itemView.findViewById(R.id.tv_title_content);
            tvDepartment = (TextView) itemView.findViewById(R.id.tv_department);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            tvOverdue = (TextView) itemView.findViewById(R.id.tv_overdue);
            button = (Button) itemView.findViewById(R.id.button);
            tvDate = (TextView) itemView.findViewById(R.id.tv_date);
            rlDateTime = (RelativeLayout) itemView.findViewById(R.id.rl_date_time);
        }
    }
}
